package cn.achao.csr.chat.core.mq.consumer;

import cn.achao.csr.api.model.dto.LoginMessageDTO;
import cn.achao.csr.api.model.entity.LoginUser;
import cn.achao.csr.base.constant.RockMqKeyConstants;
import cn.achao.csr.chat.core.build.WSAdapter;
import cn.achao.csr.chat.service.ChatService;
import cn.achao.csr.chat.service.LoginService;
import cn.achao.csr.security.service.TokenService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * Description: 在本地服务上找寻对应channel，将对应用户登陆，并触发所有用户收到上线事件
 * Author: achao
 */
@Slf4j
@Component
@RequiredArgsConstructor
@RocketMQMessageListener(consumerGroup = RockMqKeyConstants.LOGIN_MSG_GROUP, topic = RockMqKeyConstants.LOGIN_MSG_TOPIC, messageModel = MessageModel.BROADCASTING)
public class MsgLoginConsumer implements RocketMQListener<LoginMessageDTO> {

    private final ChatService chatService;
    private final TokenService tokenService;
    private final LoginService loginService;

    @Override
    public void onMessage(LoginMessageDTO loginMessageDTO) {
        // 登录
        log.info("登录消息:{}", loginMessageDTO);
        LoginUser loginUser = loginService.loginByWx(loginMessageDTO.getUid());
        chatService.sendToUid(WSAdapter.buildTokenResp(tokenService.createToken(loginUser)), loginMessageDTO.getTouristsId());
    }

}
